	var ShoppingCart=null;
	var totalAmount=0;
	var red_money=0;//红包使用金额
	var userMoney=0;
	var cartList = null;
	var user = null;
	var wx = null;
	   	var jingpay = null;
	var balance_i = null;
	var balance_pay = null;
	var box = 'balance';
	var red_type = 'N';//Y直减红包 N/满减红包
	var other_paynum = null;
	var iswx=false;
	
	
apiready = function(){
	
	var systemType = api.systemType;
	api.ajax({
        url:'http://wap.jqmao.cn/app/mobile/getpayConfig'
    },function(ret,err){
  
    	//coding...
    	switch(ret.value){
    		case '1':
    			iswx = false;
    		break;
    		case '2':
    			iswx = false;
    		break;
    	}
    });
	
	if(systemType=='ios'){
		 var wx = api.require('wx');
	     wx.isInstalled(function(ret, err) {
		 if(ret.installed) {
		      //alert("当前设备已安装微信客户端");
		     iswx = true;
		  } else {
		       iswx = false;
		  }
		});			
	}else{
		iswx = true;
	}
	

//监听切换红包
api.addEventListener({
    name: 'red_change'
}, function(ret, err) {


    $api.setStorage('c_id',ret.value.id);
    var a =$api.byId('redpacket-content');
				var tpl = $api.byId('redpacket-template').text;
				var tempFn = doT.template(tpl);	
				red_money = ret.value.money;
				
				if(red_money==totalAmount){
					red_type='Y';
				}else{
					if(userMoney==0){
   				var other = $api.byId("other-pay");
   					$api.css(other,'display:block');
   					var wx = $api.byId('radio-1-1');
   					$api.html(other_paynum,'￥'+(totalAmount-red_money));
   					$api.addCls(wx,'select')
   				}else{
   					if(userMoney<totalAmount){
   					var other = $api.byId("other-pay");
   					$api.css(other,'display:block');
   					var wx = $api.byId('radio-1-1');
   				
   					$api.addCls(wx,'select')
   					$api.html(other_paynum,'￥'+(totalAmount-userMoney-red_money));
   				}else{
   					$api.html(balance_pay,'￥'+(totalAmount-red_money));
   				}
   			}
				}
				
				
				$api.html(a,tempFn(ret.value));
});

//监听不使用红包
api.addEventListener({
    name: 'update'
}, function(ret, err) {
    var pay_content = $api.byId('pay-content');
	$api.css(pay_content,'display:block');
				red_type = 'N';
				
				var a =$api.byId('redpacket-content');
				var tpl = $api.byId('redpacket-template').text;
				var tempFn = doT.template(tpl);			
				
   				if(userMoney==0){
   				var other = $api.byId("other-pay");
   					$api.css(other,'display:block');
   					var wx = $api.byId('radio-1-1');
   					$api.html(other_paynum,'￥'+totalAmount);
   					$api.addCls(wx,'select')
   				}else{
   					if(userMoney<totalAmount){
   				
   					$api.html(other_paynum,'￥'+(totalAmount-userMoney));
   				}else{
   					$api.html(balance_pay,'￥'+totalAmount);
   				}
   				}
   				
   				
   				red_money=0;
				$api.html(a,tempFn('1'));
});
headerstatus()
	
	ShoppingCart = $api.getStorage('ShoppingCart');
		user = $api.getStorage('user');		
		userMoney = user.money;
		cartList = JSON.parse(ShoppingCart.substr(1,ShoppingCart.length));
		totalAmount = parseInt(cartList.totalAmount);//购买总需金额
		$api.setStorage('totalAmount',totalAmount);
		setTimeout(function(){showredPacket()},500)	   	   
	}



	//红包使用
	function showredPacket(){
		var uid = $api.getStorage('user').uid;
		
		api.ajax({
	        url:'http://wap.jqmao.cn/getUsable',
	        method: 'post',
	        data: {
	            values: {totalAmount:totalAmount,uid:user.uid}
	        }
	        
        },function(ret,err){
      			
       			if(ret){
       				red_money = ret.money;
       				if(red_money==totalAmount){
       					
       					//red_money = 0;
       					//ret = '';
       					red_type='Y';
       					$api.setStorage('c_id',ret.id);
       					
       				}
       				
       			}
        		
        		showContent();
        		var a =$api.byId('redpacket-content');
				var tpl = $api.byId('redpacket-template').text;
				var tempFn = doT.template(tpl);
				
        	//coding...
        	if(ret){
        	if(red_money>totalAmount){
        		red_money = 0;
				$api.html(a,tempFn(''));
				
			}else{
				$api.html(a,tempFn(ret));
			}
				
				
				
        	}else{
        	$api.html(a,tempFn(err));
        		$api.html(a,tempFn(''));
        	}
        });
        
	
		
	}

	function submit(){ 
	       getData()
	 
	}
	function showContent(){

		
		cartList['money'] = userMoney;

		cartList['iswx'] = iswx;
	
		var a =$api.byId('order-content');
		var tpl = $api.byId('order-template').text;
		var tempFn = doT.template(tpl);
		$api.html(a,tempFn(cartList));
		
		money = parseInt(user.money);//账户余额	
		
		
		var balance = $api.byId('radio-1-0');
	   	var wx = $api.byId('radio-1-1');
	   	var jingpay = $api.byId('radio-1-2');
	   	var c_pay = null;
	   		other_paynum = $api.byId('other-paynum');
			balance_i = $api.byId('balance_i');
			balance_pay = $api.byId('balance-pay');
	   	if(money==0){
	   
	   		box = 'wx';
	   		$api.remove(balance_i);
	   		$api.remove(balance_pay);

	   	}
	   	//余额小于总金额
		if(money<totalAmount){
			if(red_type=='Y'){
				var pay_content = $api.byId('pay-content');
				$api.css(pay_content,'display:none');
				box = 'balance';
			}else{	
				
			if(money==0){
				box = 'wx';
			}else{
				box = 'wx2';
			}
					
			$api.html(balance_pay,'￥'+money);
			if(red_money>totalAmount){
			c_pay = totalAmount - money ;
			}else{
			 c_pay = totalAmount - money- red_money
			}
			$api.html(other_paynum,'￥'+c_pay);
			$api.removeCls(balance, 'select');
			$api.addCls(wx,'select');
			$api.addCls(balance,'select');
			var other = $api.byId("other-pay");
			$api.css(other,'display:block');
			var sel = $api.dom('.select');				
			}
		}else{
		//余额大于总金额
			//alert(red_money)
			if(red_type=='Y'){
				var pay_content = $api.byId('pay-content');
				$api.css(pay_content,'display:none');
			}else{
			if(red_money>totalAmount){
				c_pay = totalAmount;
			}else{
				c_pay = totalAmount - red_money;
			}
			
			$api.html(balance_pay,'￥'+c_pay);
			}
			var systemType = api.systemType;

			
		}
		//radio click 
		   var button = $api.byId('order-content');
		   var balance = $api.byId('radio-1-0');
   
   
   
	   var a = null;
	   $api.addEvt(button,'click',function(e){
	   	

		if(e.target.getAttribute('data-pro')=='balance'){
			if(e.target.getAttribute('data-pro')=='balance'&&money>=totalAmount){
   			  c_pay = totalAmount - red_money;
			$api.html(other_paynum,'￥'+c_pay);
   			
   			/*$api.attr(wx,'checked','false');
   			$api.attr(jingpay,'checked','false')*/
   			if($api.hasCls(balance, 'select')){
   				$api.css(balance_pay,'display:none');
				$api.removeCls(balance, 'select');
				box = 'wx';
				
				$api.addCls(wx,'select');
				var other = $api.byId("other-pay");
   				$api.css(other,'display:block');
   			}else{	
   				box = 'balance';
   				$api.css(balance_pay,'display:block');
   				$api.addCls(balance,'select');
   				$api.removeCls(wx,'select');
   				$api.removeCls(jingpay,'select');
   				var other = $api.byId("other-pay");
   				$api.css(other,'display:none');
   			}
   			
   			
   			/*$api.attr(balance,'checked','true');*/
   			/* a = $("input[name='radio-1-set']:checked").val();*/
   		}else if(e.target.getAttribute('data-pro')=='balance'&&money<totalAmount){

   			if($api.hasCls(balance, 'select')){
   				
   				$api.css(balance_pay,'display:none');
				$api.removeCls(balance, 'select');
				if($api.hasCls(wx, 'select')){
				
					box = 'wx';
   					$api.removeCls(jingpay, 'select');
   				}
   				if($api.hasCls(jingpay, 'select')){
   					box = 'jingpay';
   					
   					$api.removeCls(wx, 'select');
   				}
				var other = $api.byId("other-pay");
   				$api.css(other,'display:block');
   				var _a = totalAmount-red_money
   				$api.html(other_paynum,'￥'+_a);
   			}else{	
   				$api.css(balance_pay,'display:block');
   				$api.addCls(balance,'select');
   				
   				if($api.hasCls(wx, 'select')){
   					box = 'wx2';
   					
   					$api.removeCls(jingpay, 'select');
   				}
   				if($api.hasCls(jingpay, 'select')){
   					box = 'jingpay2';
   					
   					$api.removeCls(wx, 'select');
   				}
   				//$api.removeCls(jingpay,'select');
   				$api.html(other_paynum,'￥'+c_pay);
   				
   			}
   		}
		}else{

			if(e.target.getAttribute('data-pro')=='wx'){
				if($api.hasCls(balance, 'select')){
					box = 'wx2';
				}else{
					box = 'wx';
				}
							
	   			
		   		if(money<totalAmount){
		   			
		   			$api.html(balance_pay,'￥'+money);
		   		}else{
	   				$api.css(balance_pay,'display:none');
	   			}
   			
	   			/*$api.attr(jingpay,'checked','false')
	   			$api.attr(balance,'checked','false');*/
	   			
	   			$api.addCls(wx,'select');
				$api.removeCls(jingpay,'select');
	   			a = $("input[name='radio-1-set']:checked").val();
   			
   		}else if(e.target.getAttribute('data-pro')=='jingpay'){
   				if($api.hasCls(balance, 'select')){
					box = 'jingpay2';
				}else{
					box = 'jingpay';
				}
   		
	   		if(money<totalAmount){
	   			$api.html(balance_pay,'￥'+money);
	   		}else{
   				$api.css(balance_pay,'display:none');
   			}
   			/*$api.attr(wx,'checked','false');
   			$api.attr(balance,'checked','false');*/
   			$api.removeCls(wx,'select');
   			$api.addCls(jingpay,'select');

   			 a = $("input[name='radio-1-set']:checked").val();

   		}
		}
   		

   })

	}
   function clickArrow(){
   		var arrow = $api.byId('arrow');
   		$api.toggleCls(arrow, 'arrow-top');
	   	var b = $api.hasCls(arrow, 'arrow-top');
   		var orderList = $api.byId('orderList');
		if(b){
			$api.css(orderList,'display:block');
		}else{
			$api.css(orderList,'display:none');
		}
		$api.toggleCls(arrow, 'arrow-bottom');
   }
   //调起浏览器支付
   function browserpay(ordercode){
   		var u ='http://wap.jqmao.cn/browserpay.html';
							api.openApp({
				                iosUrl: u,
				                appParam:{
				                	'code':''+ordercode
				                }
				               
			                },function(ret,err){
			                	//coding...
			                	
			                });
   }
   

   //发送订单
 
   function getData(){
   $api.rmStorage('totalAmount');
   $api.rmStorage('c_id');
	var totalAmount = cartList.totalAmount;//购买总需金额
	var usermoney = user.money;
	var money = 1;//充值金额
	var paytype = null;
   	var sel = $api.dom('.select');
   	var checkbox =null;
   	var paytype = null;
   	var red_money = $api.byId('red_money');
   	var red_money_id =  $api.attr(red_money,'data');
  
   	if(box=='balance'){
   		checkbox = true;
   		paytype = null;
   	}else{
   		if(box=='wx2'){
   			checkbox = true;
   			paytype = 13;
   		}else if(box=='jingpay2'){
   			checkbox = true;
   			paytype = 14;
   		}else if(box=='wx'){
   			checkbox = '';
   			paytype = 13;
   			
   		}else if(box=='jingpay'){
   			checkbox = '';
   			paytype = 14;
   		}
   		
   	}
  
   	


	//alert(paytype)
   	if(sel=='balance'){
   		paytype = money;
   	}		
   	
   			if(red_money_id==''){
   				red_money_id=0;
   			}
   				//alert(red_money_id)
   				//alert(box)
   				//alert(totalAmount)
			//alert(red_money_id)
			var productlist = cartList['productlist'];
			var uid = user.uid;
			
						
			api.ajax({
                        url: 'http://wap.jqmao.cn/setCartList',
                        method: 'post',
                        data: {
                            values: {productlist:productlist,totalAmount:totalAmount,paytype:paytype,uid:uid,checkbox:checkbox,redid:red_money_id}
                        }
            },function(ret,err){
            	if(ret){
            	//alert(paytype)
            	var ordercode = ret.code;
            	if(paytype==14){
            		
            		var remark = 'jingpay';
            		
            		var systemType = api.systemType;
            		 if(systemType=='ios'){
						browserpay(ordercode);
						var dialogBox = api.require('dialogBox');
						dialogBox.alert({
						    texts: {
						       
						        content: '支付中',
						        leftBtnTitle: '支付遇到问题',
						        rightBtnTitle: '支付成功'
						    },
						    styles: {
						        bg: '#fff',
						        w: 300,
						       
						        content: {
						            color: '#000',
						            size: 18
						        },
						        left:{      
						            marginB: 7,
						            marginL: 18,
						            w: 130,
						            h: 35,
						            corner: 2,        
						            bg: '#fff',     
						            color: '#007FFF',
						            size: 14
						        },
						        right: {    
						            marginB: 7,   
						            marginL: 18,
						            w: 130,  
						            h: 35,   
						            corner: 2,  
						            bg: '#fff',
						            color: '#007FFF',  
						            size: 14              
						        }
						    }
						}, function(ret) {
						    if(ret.eventType == 'left') {  
						        dialogBox.close({
						            dialogName: 'alert'
						        });
						    }else{
						    	//alert(ordercode);
						    	api.ajax({
	                                url:'http://wap.jqmao.cn/appGetOrderData/?code='+ordercode
                                },function(ret,err){
                                	//coding...
                                	
                                	//alert(JSON.stringify(ret))
                                	if(ret.code==0){
                                		alert('获取支付结果失败，请重试!')
                                	}else{
                                	
                                		 if (ret.redcode) {
                                		 api.openWin({
								                name: 'payResult',
								                url: 'widget://html/payResult.html',
								                pageParam: {
								                    r_code: '' + ret.redcode
								                },
								                animation: {
								                    type: "none",                //动画类型（详见动画类型常量）
								                }
								            });
											
								           
								        } else {
								         
								       
								            api.openWin({
								                name: 'payResult',
								                url: 'widget://html/payResult.html',
								                animation: {
								                    type: "none",                //动画类型（详见动画类型常量）
								                }
								
								            });
								        }
                                	}
                                	
                                });
						    }
						});
				    }else{
				    	openUrl('http://wap.jqmao.cn/apppaysubmit/?uid='+uid+'&checkbox='+checkbox+'&paytype='+paytype+'&redid='+red_money_id+'&money='+totalAmount ,'支付',remark)
				    }
            	 	}else if(paytype==13){
            
            		var remark = 'wx';
            		var systemType = api.systemType;
            		 if(systemType=='ios'){
					
				    	//coding...
				    	//alert(ret.value)
				    	
				    	
				    	//alert(ret.value)
				    	//alert(ordercode)				    			    	 				    	 			                
				  
				   
					    if (iswx) {
					        //alert("当前设备已安装微信客户端");
					        api.ajax({
	                            url:'http://wap.jqmao.cn/getpay'
                            },function(ret,err){
                            	//coding...
                            	if(ret.show=='1'){
                            		openUrl('http://wap.jqmao.cn/apppaysubmit/?uid='+uid+'&checkbox='+checkbox+'&paytype='+ret.pay_id+'&redid='+red_money_id+'&money='+totalAmount ,'支付',remark)
                            	}else{
                            		openUrl('http://wap.jqmao.cn/apppaysubmit/?uid='+uid+'&checkbox='+checkbox+'&paytype='+ret.pay_id+'&redid='+red_money_id+'&money='+totalAmount ,'支付','wx2')
                            	}
                            });
					        
					    } else {
					        //alert('当前设备未安装微信客户端');
					        browserpay(ordercode);
					        //return false;
					    }
				
				    
				}else{
				if (iswx) {
					        //alert("当前设备已安装微信客户端");
					        api.ajax({
	                            url:'http://wap.jqmao.cn/getpay'
                            },function(ret,err){
                            	//coding...
                            	if(ret.show=='1'){
                            		openUrl('http://wap.jqmao.cn/apppaysubmit/?uid='+uid+'&checkbox='+checkbox+'&paytype='+ret.pay_id+'&redid='+red_money_id+'&money='+totalAmount ,'支付',remark)
                            	}else{
                            		openUrl('http://wap.jqmao.cn/apppaysubmit/?uid='+uid+'&checkbox='+checkbox+'&paytype='+ret.pay_id+'&redid='+red_money_id+'&money='+totalAmount ,'支付','wx2')
                            	}
                            });
					        
					    } else {
					    	paytype=14
					       openUrl('http://wap.jqmao.cn/apppaysubmit/?uid='+uid+'&checkbox='+checkbox+'&paytype='+paytype+'&redid='+red_money_id+'&money='+totalAmount ,'支付','jingpay')
					    }
					
				}
            		
            	}else{
            		var remark = 'balance';
            
			
					location.href='http://wap.jqmao.cn/apppaysubmit/?uid='+uid+'&checkbox='+checkbox+'&paytype='+paytype+'&redid='+red_money_id+'&money='+totalAmount;
				
            		
            		
            		
            	}
				
            		
            	}else{
            		////alert(JSON.stringify(err))
            	}
            	
            })
            
            
            

   }


	/*alert($("input[name='radio-1-set']:checked").val());*/

  